//
// Created by wyh on 2025-06-14 09:00.
//
#include <iostream>
#include <sstream>
#include "utility/value.h"

using namespace std;
using namespace wyh::utility;

Value::Value() : m_type(V_NULL)
{

}

Value::Value(bool value) : m_type(V_BOOL)
{
    m_value = value ? "true" : "false";
}

Value::Value(int value) : m_type(V_INT)
{
    m_value = to_string(value);
}

Value::Value(unsigned int value) : m_type(V_INT)
{
    m_value = to_string(value);
}

Value::Value(float value) : m_type(V_FLOAT)
{
    m_value = to_string(value);
}

Value::Value(double value) : m_type(V_DOUBLE)
{
    m_value = to_string(value);
}

Value::Value(char *value) : m_type(V_STRING)
{
    m_value = value;
}

Value::Value(std::string value) : m_type(V_STRING)
{
    m_value = value;
}

void Value::show() const
{
    string type = "null";
    switch (m_type)
    {
        case V_BOOL:
            type = "bool";
            break;
        case V_INT:
            type = "int";
            break;
        case V_FLOAT:
            type = "float";
            break;
        case V_DOUBLE:
            type = "double";
            break;
        case V_STRING:
            type = "string";
            break;
        default:
            break;
    }

    cout << "type=" << type << ",value=" << m_value << endl;
}

Value &Value::operator=(bool value)
{
    m_type = V_BOOL;
    m_value = value?"true":"false";

    return *this;
}

Value &Value::operator=(int value)
{
    m_type = V_INT;
    m_value = to_string(value);

    return *this;
}

Value &Value::operator=(unsigned int value)
{
    m_type = V_INT;
    m_value = to_string(value);

    return *this;
}

Value &Value::operator=(float value)
{
    m_type = V_FLOAT;
    m_value = to_string(value);

    return *this;
}

Value &Value::operator=(double value)
{
    m_type = V_DOUBLE;
    m_value = to_string(value);

    return *this;
}

Value &Value::operator=(char *value)
{
    m_type = V_STRING;
    m_value = value;

    return *this;
}

Value &Value::operator=(std::string value)
{
    m_type = V_STRING;
    m_value = value;

    return *this;
}

Value::Type Value::type() const
{
    return m_type;
}

bool Value::is_null() const
{
    return m_type==V_NULL;
}

bool Value::is_bool() const
{
    return m_type==V_BOOL;
}

bool Value::is_int() const
{
    return m_type==V_INT;
}

bool Value::is_float() const
{
    return m_type==V_FLOAT;
}

bool Value::is_double() const
{
    return m_type==V_DOUBLE;
}

bool Value::is_string() const
{
    return m_type==V_STRING;
}

bool Value::operator==(const Value &other)
{
    if(m_type!=other.m_type)
    {
        return false;
    }
    return m_value==other.m_value;
}

bool Value::operator!=(const Value &other)
{
    return !(*this==other);
}

Value::operator bool()
{
    return m_value=="true";
}

Value::operator bool() const
{
    return m_value=="true";
}

Value::operator int()
{
    int value;
    stringstream ss;
    ss<<m_value;
    ss>>value;

    return value;
}

Value::operator int() const
{
    int value;
    stringstream ss;
    ss<<m_value;
    ss>>value;

    return value;
}

Value::operator unsigned int()
{
    int value;
    stringstream ss;
    ss<<m_value;
    ss>>value;

    return value;
}

Value::operator unsigned int() const
{
    unsigned int value;
    stringstream ss;
    ss<<m_value;
    ss>>value;

    return value;
}

Value::operator float()
{
    float value;
    stringstream ss;
    ss<<m_value;
    ss>>value;

    return value;
}

Value::operator float() const
{
    float value;
    stringstream ss;
    ss<<m_value;
    ss>>value;

    return value;
}

Value::operator double()
{
    double value;
    stringstream ss;
    ss<<m_value;
    ss>>value;

    return value;
}

Value::operator double() const
{
    double value;
    stringstream ss;
    ss<<m_value;
    ss>>value;

    return value;
}

Value::operator std::string()
{
    return m_value;
}

Value::operator std::string() const
{
    return m_value;
}

